#include "LogicControlXml.h"
#include "QObject"
LogicControlXml::LogicControlXml()
{
    m_acLogicControlList.clear();
    m_heatLogicControlList.clear();
}
bool LogicControlXml::parser()
{
    if(!this->check())
    {
        return false;
    }
    QFile file(m_path);
    file.open(QFile::ReadOnly | QFile::Text);
    QDomDocument doc;
    doc.setContent(&file);
    file.close();

    QDomElement root = doc.documentElement();
    QDomNode rootChild = root.firstChild();

    SavePointXml *savePointXml =SavePointXml::getInstance();
    while (!rootChild.isNull())
    {
        if(rootChild.toElement().tagName() == "heatTemp")   //解析场景点位
        {
            HeatLogicControlStruct heatLogicControl;
            ControlPointStruct tempPoint;

            QDomNode temperatureChild = rootChild.toElement().firstChild();
            while(!temperatureChild.isNull())
            {
                if (temperatureChild.toElement().tagName() == "closePT")
                {
                    QStringList strTemp = temperatureChild.toElement().text().split(",");
                    tempPoint.setAddress(strTemp.at(0).toInt());
                    tempPoint.setValue(strTemp.at(1).toInt());
                    heatLogicControl.setClosePoint(tempPoint);
                    if(savePointXml->AutomaticContrast(tempPoint.address(),tempPoint.value())==true)
                    {
                        heatLogicControl.setIsOpen(false);
                    }
                }
                else if (temperatureChild.toElement().tagName() == "openPT")
                {
                    QStringList strTemp = temperatureChild.toElement().text().split(",");
                    tempPoint.setAddress(strTemp.at(0).toInt());
                    tempPoint.setValue(strTemp.at(1).toInt());
                    heatLogicControl.setOpenPoint(tempPoint);
                    qDebug()<<"this openPT addr="<<tempPoint.address();
                    qDebug()<<"this openPT value="<<tempPoint.value();
                    if(savePointXml->AutomaticContrast(tempPoint.address(),tempPoint.value())==true)
                    {
                        heatLogicControl.setIsOpen(true);
                        qDebug()<<"is true old going true";
                    }
                }
                else if (temperatureChild.toElement().tagName() == "curAddress")
                {
                    heatLogicControl.setCurTempAddress(temperatureChild.toElement().text().toInt());
                    if(savePointXml->findData(heatLogicControl.curTempAddress())==true)
                    {
                        quint16 curdata=savePointXml->GetSavePointValue(heatLogicControl.curTempAddress());
                        heatLogicControl.setCurTempdata(curdata);
                        qDebug()<<"get curdata";
                    }
                }
                else if(temperatureChild.toElement().tagName() == "setAddress")
                {
                    heatLogicControl.setSetTempAddress(temperatureChild.toElement().text().toInt());
                }
                else if(temperatureChild.toElement().tagName() == "setValue")
                {
                    heatLogicControl.setSetTempValue(temperatureChild.toElement().text().toInt());
                }
                else if(temperatureChild.toElement().tagName() == "maxErrorRange")
                {
                    QString maxErrorRange = temperatureChild.toElement().text();
                    heatLogicControl.setMaxerrorRange(maxErrorRange);
                    qDebug()<<"maxerrorRange="<<maxErrorRange;
                }
                else if(temperatureChild.toElement().tagName() == "minErrorRange")
                {
                    QString minErrorRange = temperatureChild.toElement().text();
                    heatLogicControl.setMinerrorRange(minErrorRange);
                    qDebug()<<"minerrorRange="<<minErrorRange;
                  //  heatLogicControl.setErrorRange(temperatureChild.toElement().text().toInt());
                }
                else if(temperatureChild.toElement().tagName() == "greater")
                {
                    QStringList strTemp = temperatureChild.toElement().text().split(",");
                    tempPoint.setAddress(strTemp.at(0).toInt());
                    tempPoint.setValue(strTemp.at(1).toInt());
                    heatLogicControl.setGreaterPoint(tempPoint);
                }
                else if(temperatureChild.toElement().tagName() == "less")
                {
                    QStringList strTemp = temperatureChild.toElement().text().split(",");
                    tempPoint.setAddress(strTemp.at(0).toInt());
                    tempPoint.setValue(strTemp.at(1).toInt());
                    heatLogicControl.setLessPoint(tempPoint);
                }
                temperatureChild = temperatureChild.nextSibling();
            }
            m_heatLogicControlList.append(heatLogicControl);
        }

        if(rootChild.toElement().tagName() == "acTemp")   //解析场景点位
        {
            AcLogicControlStruct acLogicControl;
            QList<ModeStruct> modeList;
            ControlPointStruct tempPoint;

            QDomNode temperatureChild = rootChild.toElement().firstChild();
            while(!temperatureChild.isNull())
            {
                if (temperatureChild.toElement().tagName() == "closePT")
                {
                    QStringList strTemp = temperatureChild.toElement().text().split(",");
                    tempPoint.setAddress(strTemp.at(0).toInt());
                    tempPoint.setValue(strTemp.at(1).toInt());
                    acLogicControl.setClosePoint(tempPoint);
                }
                else if (temperatureChild.toElement().tagName() == "openPT")
                {
                    QStringList strTemp = temperatureChild.toElement().text().split(",");
                    tempPoint.setAddress(strTemp.at(0).toInt());
                    tempPoint.setValue(strTemp.at(1).toInt());
                    acLogicControl.setOpenPoint(tempPoint);
                }
                else if (temperatureChild.toElement().tagName() == "curAddress")
                {
                    acLogicControl.setCurTempAddress(temperatureChild.toElement().text().toInt());
                }
                else if(temperatureChild.toElement().tagName() == "setAddress")
                {
                    acLogicControl.setSetTempAddress(temperatureChild.toElement().text().toInt());
                }
                else if(temperatureChild.toElement().tagName() == "setValue")
                {
                    acLogicControl.setSetTempValue(temperatureChild.toElement().text().toInt());
                }
                else if(temperatureChild.toElement().tagName() == "errorRange")
                {
                    acLogicControl.setErrorRange(temperatureChild.toElement().text().toInt());
                }
                else if(temperatureChild.toElement().tagName() == "errorRange")
                {
                    acLogicControl.setErrorRange(temperatureChild.toElement().text().toInt());
                }
                else if(temperatureChild.toElement().tagName() == "curMode")
                {
                    acLogicControl.setCurMode(temperatureChild.toElement().text());
                }

                else if(temperatureChild.toElement().tagName() == "mode")
                {
                    ModeStruct mode;
                    mode.setName(temperatureChild.toElement().attribute("name"));

                    QDomNode modeChild = temperatureChild.toElement().firstChild();
                    while(!modeChild.isNull())
                    {
                        if(modeChild.toElement().tagName() == "modePT")
                        {
                            QStringList strTemp = modeChild.toElement().text().split(",");
                            tempPoint.setAddress(strTemp.at(0).toInt());
                            tempPoint.setValue(strTemp.at(1).toInt());
                            mode.setModePoint(tempPoint);
                        }
                        if(modeChild.toElement().tagName() == "greater")
                        {
                            QStringList strTemp = modeChild.toElement().text().split(",");
                            tempPoint.setAddress(strTemp.at(0).toInt());
                            tempPoint.setValue(strTemp.at(1).toInt());
                            mode.setGreaterPoint(tempPoint);
                        }
                        if(modeChild.toElement().tagName() == "less")
                        {
                            QStringList strTemp = modeChild.toElement().text().split(",");
                            tempPoint.setAddress(strTemp.at(0).toInt());
                            tempPoint.setValue(strTemp.at(1).toInt());
                            mode.setLessPoint(tempPoint);
                        }
                        modeChild = modeChild.nextSibling();
                    }
                    modeList.append(mode);
                }
                temperatureChild = temperatureChild.nextSibling();
            }
            acLogicControl.setModeList(modeList);
            m_acLogicControlList.append(acLogicControl);
        }

        rootChild = rootChild.nextSibling();
    }
    return true;
}

bool LogicControlXml::clear()
{
    if(!this->clearAllNode())
    {
        return false;
    }
    m_heatLogicControlList.clear();
    m_acLogicControlList.clear();

    return true;
}

QList<ControlPointStruct> LogicControlXml::controlPointList(const ControlPointStruct &controlPoint)
{
    QList<ControlPointStruct> controlPointList;

    for(int i=0; i<m_heatLogicControlList.size(); i++)
    {
        HeatLogicControlStruct heatLogicControl = m_heatLogicControlList.at(i);
        if(heatLogicControl.closePoint() == controlPoint)
        {
            m_heatLogicControlList[i].setIsOpen(false);
            m_heatLogicControlList[i].setSWStata(2);
            controlPointList.append(heatLogicControl.greaterPoint());
        }
        else if(heatLogicControl.openPoint() == controlPoint)
        {
            m_heatLogicControlList[i].setIsOpen(true);
          //  m_heatLogicControlList[i].setSWStata(1);
           //  controlPointList.append(heatLogicControl.lessPoint());
//            quint16 tempMin = heatLogicControl.setTempValue() - heatLogicControl.errorRange();
//            quint16 tempMax = heatLogicControl.setTempValue() + heatLogicControl.errorRange();
             quint16 tempMax;
             quint16 tempMin;
            if(heatLogicControl.getMaxerrorRange().mid(0)=="-")
            {
                tempMax = heatLogicControl.setTempValue() +heatLogicControl.getMaxerrorRange().remove("-").toInt();
            }else
            {
                tempMax=heatLogicControl.setTempValue() -heatLogicControl.getMaxerrorRange().toInt();
            }
            if(heatLogicControl.getMinerrorRange().mid(0)=="-")
            {
                tempMin = heatLogicControl.setTempValue() +heatLogicControl.getMinerrorRange().remove("-").toInt();
            }else
            {
                tempMin=heatLogicControl.setTempValue() -heatLogicControl.getMinerrorRange().toInt();
            }


             qDebug()<<"setTempvalue="<<QString::number(heatLogicControl.setTempValue());
          //  qDebug()<<"errorRange="<<QString::number(heatLogicControl.errorRange());
            qDebug()<<"tempMin="<<QString::number(tempMin);
            qDebug()<<"tempMax="<<QString::number(tempMax);
            qDebug()<<"controlPoint-Value="<<QString::number(controlPoint.value());
             qDebug()<<"getCurdata="<<QString::number(heatLogicControl.getCurTempdata());
            if(heatLogicControl.getCurTempdata() <= tempMin)
            {
                qDebug()<<"<=tempMIN";
                if(heatLogicControl.getSWStata()!=1)
                {
                controlPointList.append(heatLogicControl.lessPoint());
                controlPointList.append(heatLogicControl.lessPoint());
                controlPointList.append(heatLogicControl.lessPoint());
                m_heatLogicControlList[i].setSWStata(1);
                }
            }
            else if(heatLogicControl.getCurTempdata()>= tempMax)
            {
                if(heatLogicControl.getSWStata()!=2)
                {
                qDebug()<<"<=tempMax";
                controlPointList.append(heatLogicControl.greaterPoint());
                controlPointList.append(heatLogicControl.greaterPoint());
                controlPointList.append(heatLogicControl.greaterPoint());
                m_heatLogicControlList[i].setSWStata(2);
                }
            }
        }
        else if(heatLogicControl.setTempAddress() == controlPoint.address())
        {
            qDebug()<<"set address now="<<QString::number(controlPoint.value());
            heatLogicControl.setSetTempValue(controlPoint.value());
            m_heatLogicControlList[i].setSetTempValue(controlPoint.value());
            setsetValue(controlPoint.address(),controlPoint.value());
            if(heatLogicControl.isOpen())
            {
                quint16 tempMax;
                quint16 tempMin;
               if(heatLogicControl.getMaxerrorRange().mid(0)=="-")
               {
                   tempMax = heatLogicControl.setTempValue() +heatLogicControl.getMaxerrorRange().remove("-").toInt();
               }else
               {
                   tempMax=heatLogicControl.setTempValue() -heatLogicControl.getMaxerrorRange().toInt();
               }
               if(heatLogicControl.getMinerrorRange().mid(0)=="-")
               {
                   tempMin = heatLogicControl.setTempValue() +heatLogicControl.getMinerrorRange().remove("-").toInt();
               }else
               {
                   tempMin=heatLogicControl.setTempValue() -heatLogicControl.getMinerrorRange().toInt();
               }
                qDebug()<<"tempMin="<<QString::number(tempMin);
                qDebug()<<"tempMax="<<QString::number(tempMax);
                qDebug()<<"controlPoint-Value="<<QString::number(controlPoint.value());
                qDebug()<<"getCurdata="<<QString::number(heatLogicControl.getCurTempdata());
                if(heatLogicControl.getCurTempdata() <= tempMin)
                {
                    qDebug()<<"<=tempMin";
                    if(heatLogicControl.getSWStata()!=1)
                    {
                    controlPointList.append(heatLogicControl.lessPoint());
                    controlPointList.append(heatLogicControl.lessPoint());
                    controlPointList.append(heatLogicControl.lessPoint());
                    m_heatLogicControlList[i].setSWStata(1);
                    }
                }
                else if(heatLogicControl.getCurTempdata()>= tempMax)
                {
                    qDebug()<<"<=tempMax";
                    if(heatLogicControl.getSWStata()!=2)
                    {
                    qDebug()<<"turn the greaterpoint";
                    controlPointList.append(heatLogicControl.greaterPoint());
                    controlPointList.append(heatLogicControl.greaterPoint());
                    controlPointList.append(heatLogicControl.greaterPoint());
                    m_heatLogicControlList[i].setSWStata(2);
                    }
                }
            }
        }
        else if(heatLogicControl.curTempAddress()  == controlPoint.address())
        {
           m_heatLogicControlList[i].setCurTempdata(controlPoint.value());
            if(heatLogicControl.isOpen())
            {
                quint16 tempMax;
                quint16 tempMin;
               if(heatLogicControl.getMaxerrorRange().mid(0)=="-")
               {
                   tempMax = heatLogicControl.setTempValue() +heatLogicControl.getMaxerrorRange().remove("-").toInt();
               }else
               {
                   tempMax=heatLogicControl.setTempValue() -heatLogicControl.getMaxerrorRange().toInt();
               }
               if(heatLogicControl.getMinerrorRange().mid(0)=="-")
               {
                   tempMin = heatLogicControl.setTempValue()+heatLogicControl.getMinerrorRange().remove("-").toInt();
               }else
               {
                   tempMin=heatLogicControl.setTempValue()-heatLogicControl.getMinerrorRange().toInt();
               }
                qDebug()<<"tempMin="<<QString::number(tempMin);
                qDebug()<<"tempMax="<<QString::number(tempMax);
                qDebug()<<"controlPoint-Value="<<QString::number(controlPoint.value());
                 qDebug()<<"getCurdata="<<QString::number(heatLogicControl.getCurTempdata());
                if(controlPoint.value() <= tempMin)
                {
                    qDebug()<<"<=tempMin";
                    if(heatLogicControl.getSWStata()!=1)
                    {
                    controlPointList.append(heatLogicControl.lessPoint());
                    controlPointList.append(heatLogicControl.lessPoint());
                    controlPointList.append(heatLogicControl.lessPoint());
                    m_heatLogicControlList[i].setSWStata(1);
                    }
                }
                else if(controlPoint.value() >= tempMax)
                {
                    qDebug()<<"<=tempMax";
                    if(heatLogicControl.getSWStata()!=2)
                    {
                    qDebug()<<"turn the greaterpoint";
                    controlPointList.append(heatLogicControl.greaterPoint());
                    controlPointList.append(heatLogicControl.greaterPoint());
                    controlPointList.append(heatLogicControl.greaterPoint());
                    m_heatLogicControlList[i].setSWStata(2);
                    }
                }
            }
        }
    }

    for(int i=0; i<m_acLogicControlList.size(); i++)
    {
        AcLogicControlStruct acLogicControl = m_acLogicControlList.at(i);
        if(acLogicControl.closePoint() == controlPoint)
        {
            m_acLogicControlList[i].setIsOpen(false);
        }
        else if(acLogicControl.openPoint() == controlPoint)
        {
            m_acLogicControlList[i].setIsOpen(true);
        }
        else if(acLogicControl.setTempAddress() == controlPoint.address())
        {
            m_acLogicControlList[i].setSetTempValue(controlPoint.value());
        }
        else if(acLogicControl.curTempAddress()  == controlPoint.address())
        {
            if(acLogicControl.isOpen())
            {
                quint16 tempMin = acLogicControl.setTempValue() - acLogicControl.errorRange();
                quint16 tempMax = acLogicControl.setTempValue() + acLogicControl.errorRange();

                QString curMode = acLogicControl.curMode();
                foreach (const ModeStruct &mode, acLogicControl.modeList())
                {
                    if(curMode == mode.name())
                    {
                        if(controlPoint.value() <= tempMin)
                        {
                            controlPointList.append(mode.lessPoint());
                        }
                        else if(controlPoint.value() >= tempMax)
                        {
                            controlPointList.append(mode.greaterPoint());
                        }
                    }
                }
            }
        }
        foreach (const ModeStruct &mode, acLogicControl.modeList())
        {
            if(mode.modePoint() == controlPoint)
            {
                m_acLogicControlList[i].setCurMode(mode.name());
            }
        }
    }
    return controlPointList;
}

QList<ControlPointStruct> LogicControlXml::controlPointList(const QList<ControlPointStruct> &sourcePointList)
{
    QList<ControlPointStruct> controlPointList;
    foreach (const ControlPointStruct &sourcePoint, sourcePointList)
    {
        controlPointList.append(this->controlPointList(sourcePoint));
    }
    return controlPointList;
}

bool LogicControlXml::setsetValue(quint16 addr,quint16 value)
{
    if(!this->check())
    {
        return false;
    }
    QFile file(m_path);
    file.open(QFile::ReadOnly | QFile::Text);
    QDomDocument doc;
    doc.setContent(&file);
    file.close();

    QDomElement root = doc.documentElement();
    QDomNode rootChild = root.firstChild();

    while (!rootChild.isNull())
    {
        if(rootChild.toElement().tagName() == "heatTemp")   //解析场景点位
        {
            QDomNodeList nodeList = rootChild.childNodes();
            for(int i=0; i<nodeList.size(); i++)
            {
                QDomNode node = nodeList.at(i);
                if(node.toElement().tagName() == "setAddress")
                {
                    QString XMLaddr = node.toElement().text();
                    QString addrdata=QString::number(addr,10);
                    if(XMLaddr==addrdata)
                    {
                        for(int a=0; a<nodeList.size(); a++)
                        {
                            QDomNode node = nodeList.at(a);
                            if(node.toElement().tagName() == "setValue")
                            {
                                QDomNode oldNode=node.toElement().firstChild();
                                QString setvalue = QString::number(value,10).toUpper() ;
                                node.toElement().firstChild().setNodeValue(setvalue);
                                QDomNode newNode=node.toElement().firstChild();
                                node.replaceChild(newNode,oldNode);
                            }
                        }
                    }

                }
            }
        }
        rootChild=rootChild.nextSibling();
    }
    file.open(QFile::WriteOnly|QFile::Truncate); //先读进来，再重写，如果不用truncate就是在后面追加内容，就无效了
    QTextStream outStream(&file);
    doc.save(outStream,4); //缩进4格
    file.flush();
    file.close();
    qDebug()<<"setsetValue="<<value;;
    return true;
}



